/*
 * File:   main.cpp
 * Author: Piotrek
 *
 * Created on 17 kwiecień 2010, 21:54
 */


#include "stdafx.h"
#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include "Form1.h"

// classes:
#include "Robot.h"
#include "StaticObject.h"
#include "Utils.h"


// zmienne globalne
bool coordinates=true;


/**
 * Rysowanie sceny za pomoca OpenGL
 */
void DrawMainScene(void)
{

        // OpenGL rendering goes here...
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
        glLoadIdentity();
		/// @todo ponizsza linijka nie wiadomo skad sie bierze
		float z = RobotCollisionDetection::Form1::TranslateScene[2]*10;
		float x = RobotCollisionDetection::Form1::TranslateScene[0];
		float y = RobotCollisionDetection::Form1::TranslateScene[1];
		glTranslatef(x,y,z); // Translate Into/Out Of The Screen By z

		float xrot = RobotCollisionDetection::Form1::RotateScene[0];
		float yrot = RobotCollisionDetection::Form1::RotateScene[1];
		float zrot = RobotCollisionDetection::Form1::RotateScene[2];
        glRotatef(xrot, 1.0f, 0.0f, 0.0f); // Rotate On The X Axis
        glRotatef(yrot, 0.0f, 1.0f, 0.0f); // Rotate On The Y Axis
        glRotatef(zrot, 0.0f, 0.0f, 1.0f); // Rotate On The Z Axis

        //----------------------------------------------------------------------

        // polozenie obserwatora
        // gluLookAt (0.0, 0.0, 0.0, 0.0, 0.0, -100.0, 0.0, 1.0, 0.0);

        glScalef(0.05f,0.05f,0.05f);

        if (coordinates)
            showCoordinates();

	#ifdef DEBUG_LEVEL
		int start, stop;
		start = timeGetTime();
	#endif

		// uwaga - najpierw trzeba rysowac obiekty nieprzezroczyste
		for (unsigned int i = 0; i < CollisionDetection::CollisionDetectionModels.size(); i++)
		{	
			// added by [KPC]
			//((StaticObject*)(CollisionDetection::CollisionDetectionModels[i]))->draw(0.2f, 0.5f, 0.4f, 1.0f);
			StaticObject& currentObj = *((StaticObject*)CollisionDetection::CollisionDetectionModels[i]);
			if (currentObj.objectColor.alpha == 1.0)
				currentObj.draw(currentObj.objectColor.R, currentObj.objectColor.G, currentObj.objectColor.B, currentObj.objectColor.alpha);
		}
		for (unsigned int i = 0; i < CollisionDetection::CollisionDetectionModels.size(); i++)
		{	
			// added by [KPC]
			//((StaticObject*)(CollisionDetection::CollisionDetectionModels[i]))->draw(0.2f, 0.5f, 0.4f, 1.0f);
			StaticObject& currentObj = *((StaticObject*)CollisionDetection::CollisionDetectionModels[i]);
			if (currentObj.objectColor.alpha < 1.0)
				currentObj.draw(currentObj.objectColor.R, currentObj.objectColor.G, currentObj.objectColor.B, currentObj.objectColor.alpha);
		}
		


	#ifdef DEBUG_LEVEL
		stop = timeGetTime();
		printf("draw=%i\t",stop-start);
	#endif

}


/**
 * Inicjacja i konfiguracja OpenGL
 */
void InitOpenGL(void)
{				

    glShadeModel(GL_SMOOTH); // Enable Smooth Shading
    glClearColor(0.2f, 0.2f, 0.3f, 0.5f); // Black Background
    glClearDepth(1.0f); // Depth Buffer Setup
    glEnable(GL_DEPTH_TEST); // Enables Depth Testing   - brak tego powoduje dziwna perspektywe
    glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations

    // lighting
    glEnable(GL_NORMALIZE); // potrzebne do normalizacji swiatla

    //GLfloat LightAmbient[]= { 1.0f, 1.0f, 0.0f, 1.0f }; // Ambient Light Values ( NEW )
    GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f }; // Diffuse Light Values ( NEW )
    GLfloat LightPosition[]= { 0.0f, 0.0f, 2.0f, 1.0f }; // Light Position ( NEW )
    //glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); // Setup The Ambient Light
    glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); // Setup The Diffuse Light
    glLightfv(GL_LIGHT1, GL_POSITION,LightPosition); // Position The Light
    glEnable(GL_LIGHT1); // Enable Light One
    glEnable(GL_LIGHTING); // Enable Lighting

    #ifdef LIGHT2
    glEnable(GL_COLOR_MATERIAL);
    #endif
	
	glEnable (GL_BLEND);	// Enable Transparency
	glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

}
